<?php
/**
  * SaorFM plugin tests - hiddenfiles
  *
  * PHP Version 5
  *
  * @category SaorFM
  * @package  None
  * @author   Kae Verens <kae@verens.com>
  * @license  http://www.opensource.org/licenses/bsd-license.php BSD License
  * @link     http://www.saorfm.org/
 */

require_once 'PHPUnit/Framework.php';
$SaorFM=new SaorFM();
require_once dirname(__FILE__).'/../../../core/SaorFM.php';

/**
  * tests for the HiddenFiles plugin
  *
  * @category SaorFM
  * @package  HiddenFiles
  * @author   Kae Verens <kae@verens.com>
  * @license  http://www.opensource.org/licenses/bsd-license.php BSD License
  * @link     http://www.saorfm.org/
**/
class SaorFM_HiddenFilesTest extends PHPUnit_Framework_TestCase{
	protected $backupGlobalsBlacklist = array('SaorFM');

	/**
	  * test the installPlugin method
	  *
	  * @return void
	  */
	public function testInstallPlugin() {
		global $SaorFM;
		// { installing with an illegal name should fail
		$this->assertEquals(
			'{"error":31}',
			$SaorFM->installPlugin('test.illegal.name')
		);
		// }
		// { installing a non-existent plugin should fail
		$this->assertEquals(
			'{"error":32,"error-params":["non-existing-plugin"]}',
			$SaorFM->installPlugin('non-existing-plugin')
		);
		// }
		// { create some files to test
		touch(SAORFM_FILES.'/visible-file');
		touch(SAORFM_FILES.'/.invisible-file');
		mkdir(SAORFM_FILES.'/.invisible-directory');
		touch(SAORFM_FILES.'/.invisible-directory/visible-file');
		// }
		// { before installtion, listFiles should show all files
		$this->assertEquals(
			'[{"n":".invisible-directory","s":4096,"d":1},'
			.'{"n":".invisible-file","s":0,"d":0},{"n":"visible-file","s":0,"d":0}]',
			$SaorFM->listFiles('/')
		);
		// }
		// { installing hidden files plugin should succeed
		$this->assertEquals(
			'{}',
			$SaorFM->installPlugin('hidden-files')
		);
		// }
		// { "hiddenfiles" value should be filled
		$this->assertEquals(
			'#^/?\.#',
			$SaorFM->get('hiddenfiles')
		);
		// }
		// { installing it again should fail (already installed)
		$this->assertEquals(
			'{"error":30}',
			$SaorFM->installPlugin('hidden-files')
		);
		// }
	}

	/**
	  * test listFiles
	  *
	  * @depends testInstallPlugin
	  *
	  * @return void
	  */
	public function testListFiles() {
		global $SaorFM;
		$this->assertEquals(
			'[{"n":"visible-file","s":0,"d":0}]',
			$SaorFM->listFiles('/')
		);
		$this->assertEquals(
			'{"error":9}',
			$SaorFM->listFiles('/.invisible-directory')
		);
	}

	/**
	  * test the uninstallPlugin method
	  *
	  * @depends testListFiles
	  *
	  * @return void
	  */
	public function testUninstallPlugin() {
		global $SaorFM;
		// { uninstalling a non-existing plugin should fail
		$this->assertEquals(
			'{"error":33}',
			$SaorFM->uninstallPlugin('non-existing plugin')
		);
		// }
		// { uninstalling an existing plugin should succeed
		$this->assertEquals(
			'{}',
			$SaorFM->uninstallPlugin('hidden-files')
		);
		// }
		// { make sure it did its cleanup
		$this->assertEquals(
			null,
			$SaorFM->get('hiddenfiles')
		);
		// }
		// { cleanup
		unlink(SAORFM_FILES.'/visible-file');
		unlink(SAORFM_FILES.'/.invisible-file');
		unlink(SAORFM_FILES.'/.invisible-directory/visible-file');
		rmdir(SAORFM_FILES.'/.invisible-directory');
		// }
	}
}
